package com.mobilereports.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.mobilereports.R;
import com.mobilereports.model.ReportChartModel;
import com.mobilereports.model.ReportColumn;

public class ColumnSelectionActivity extends Activity
{

	private Button backToReportsButton;
	private Button plotChartButton;
	private ListView allColumnsListView;
	private ListView selectedColumnsListView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.columns);	
		
		backToReportsButton = (Button) findViewById(R.id.backToReportsButton);
		backToReportsButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				finish();
			}
		});
		
		plotChartButton = (Button) findViewById(R.id.plotChartButton);
		plotChartButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				if(GlobalState.selectedColumnsArrayAdapter.isEmpty())
				{
//					AlertDialog alertDialog = 
//						new AlertDialog.Builder(getApplicationContext()).create();
//					alertDialog.setTitle("Error");
//					alertDialog.setMessage("No columns has been selected!");
//					alertDialog.show();

					return;
				}
				
				GlobalState.chartModel = new ReportChartModel(GlobalState.reportService);
				
				GlobalState.selectedColumns = new ArrayList<ReportColumn>();
				for(int c1=0; c1 < GlobalState.selectedColumnsArrayAdapter.getCount(); c1++)
					GlobalState.selectedColumns.add(GlobalState.selectedColumnsArrayAdapter.getItem(c1));
						
                Intent intent = new Intent(ColumnSelectionActivity.this,
                		ChartActivity.class);
                startActivity(intent);
			}
		});
		
		allColumnsListView = (ListView) findViewById(R.id.allColumnsListView);
		allColumnsListView.setAdapter(GlobalState.allColumnsArrayAdapter);
		allColumnsListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id)
			{
				ReportColumn reportColumn = (ReportColumn)adapterView.getItemAtPosition(position);
				GlobalState.selectedColumnsArrayAdapter.add(reportColumn);
				GlobalState.allColumnsArrayAdapter.remove(reportColumn);
			}
		});		
		
		selectedColumnsListView = (ListView) findViewById(R.id.selectedColumnsListView);
		selectedColumnsListView.setAdapter(GlobalState.selectedColumnsArrayAdapter);
		selectedColumnsListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id)
			{
				ReportColumn reportColumn = (ReportColumn)adapterView.getItemAtPosition(position);
				GlobalState.allColumnsArrayAdapter.add(reportColumn);
				GlobalState.selectedColumnsArrayAdapter.remove(reportColumn);
			}
		});
	}
}
